package com.superhelper.superhelper_web.task;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.superhelper.common.domain.TakeOutOrderType;
import com.superhelper.common.utils.JsonHelper;
import com.superhelper.superhelper_web.domain.Order;
import com.superhelper.superhelper_web.mapper.OrderMapper;
import com.superhelper.superhelper_web.service.ElemeService;
import com.superhelper.superhelper_web.service.MtService;

@Component("takeOutOrderTask")
public class TakeOutOrderTask extends AbstractTask {

	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private MtService mtService;
	@Autowired
	private ElemeService elemeService;

	@Override
	public void execute() {
		// TODO Auto-generated method stub
		List<Order> list = orderMapper.getTakeout_orderNoFinish();
		log.info("TakeOutOrderTask:{}", JsonHelper.toJson(list));
		String status = null;
		for (Order o : list) {
			try {
				if (o.getType().equals(TakeOutOrderType.EleMe.getValue())) {
					status = elemeService.getOrderStatus(o.getShopId(),
							o.getPlfOrderId());

				} else if (o.getType().equals(
						TakeOutOrderType.MeiTuan.getValue())) {
					status = mtService.getOrderStatus(o.getShopId(),
							o.getPlfOrderId());

				}
				if (StringUtils.isNotBlank(status)) {
					orderMapper.updateTakeOrderStatus(o.getId(), status);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("TakeOutOrderTask:{},{}", o.getPlfOrderId(), o);
			}
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
